﻿using Core.DataAccess.Model.Project.Order;
using Core.Framework.Model.Common;
using Core.Framework.Util;
using Core.IBusiness.IOrderModule;
using Microsoft.AspNetCore.Mvc;
using System;

namespace Core.Web.Controllers.Order
{

    /// <summary>
    /// 用户订单地址配置
    /// </summary>
    [Route(".v1/Order/[controller]")]
    [ApiController]
    [ApiVerification(IBusiness.IProjectModule.Model.APIServiceEnum.Shop)]
    public class OrderUserAddressController : BaseApiController
    {
        public OrderUserAddressController(IOrder iOrder) :
            base(iOrder: iOrder)
        { 
        
        }
        

        /// <summary>
        /// 获取用户地址信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IResult Get()
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iOrder.GetUserAddress(new OrderUserAddress
                    {
                        ProjectToken = iProjectInfo.Token,
                        UserKey = iClientInfo.Key
                    }));
                });
        }



        /// <summary>
        /// 添加地址
        /// </summary>
        /// <param name="model">地址模型</param>
        /// <returns></returns>
        [HttpPost]
        public IResult POST(OrderUserAddress model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iOrder.UserAddressExec(
                        model,
                        SqlExecType.insert));
                }, model.IsValid(IsValidEnum.reg));
        }





        /// <summary>
        /// 修改地址信息
        /// </summary>
        /// <param name="model">字典实体</param>
        /// <returns></returns>
        [HttpPut]
        public IResult PUT(OrderUserAddress model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iOrder.UserAddressExec(
                        model,
                        SqlExecType.update));
                }, model.IsValid(IsValidEnum.update));
        }




        /// <summary>
        /// 删除地址
        /// </summary>
        /// <param name="id">字典关键字</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public IResult Delete(int id)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iOrder.UserAddressExec(
                        new OrderUserAddress()
                        {
                            Id = id,
                            ProjectToken = iProjectInfo.Token
                        }, 
                        SqlExecType.delete));
                },
                new Tuple<bool, string>(id > 0, id > 0 ? "" : "params: id is null"));
        }


    }
}